package com.sias.beanpostprocessor;

import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor;
import org.springframework.context.annotation.CommonAnnotationBeanPostProcessor;
import org.springframework.context.annotation.ContextAnnotationAutowireCandidateResolver;
import org.springframework.context.support.GenericApplicationContext;

/**
 * @author 吴文杰
 * @version 1.0
 * @since 2023/5/31 22:16
 */
@SuppressWarnings("all")
public class A04Application {
  public static void main(String[] args) {
    // GenericApplicationContext 是一个干净的容器
    GenericApplicationContext context = new GenericApplicationContext();

    // 用原始方法注册三个bean
    context.registerBean("bean1", Bean1.class);
    context.registerBean("bean2", Bean2.class);
    context.registerBean("bean3", Bean3.class);
    context.registerBean("bean", Bean.class);

    context.getDefaultListableBeanFactory().setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
    context.registerBean(AutowiredAnnotationBeanPostProcessor.class); //@Autowired @Value
    context.registerBean(CommonAnnotationBeanPostProcessor.class); //@Resource @PostContruct @PreDestroy

    ConfigurationPropertiesBindingPostProcessor.register(context.getDefaultListableBeanFactory()); //@ConfigurationProperties

    // 初始化容器
    context.refresh(); // 执行beanFactory后处理器，添加bean后处理器，初始化所有单例

    Bean bean = context.getBean(Bean.class);
    System.out.println(bean);


    // 销毁容器
    context.close();
  }

}
